package com.kaka.bus.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.bean.ProductDetailBean;

import java.util.List;

public class LoanProductAdapter extends RecyclerView.Adapter<LoanProductAdapter.MyHold> {
    private Activity mActivity;
    private List<ProductDetailBean.ProductBean> mProducts;
    private int selected = -1;
    private OnItemClickLitener mItemClickLitener;

    public LoanProductAdapter(Activity mActivity, OnItemClickLitener itemClickLitener, List<ProductDetailBean.ProductBean> products) {
        this.mActivity = mActivity;
        mItemClickLitener = itemClickLitener;
        mProducts = products;
    }

    @NonNull
    @Override
    public MyHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.item_loan, null, false);

        return new MyHold(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHold myHold, int position) {
        myHold.tv_amount.setText(mProducts.get(position).getLoanAmount());
        if (mProducts.get(position).getEnabledClick().equals("1")) {
            if (selected == -1) {
                selected = position;
            }
        }
        if (selected == position) {
            myHold.rl_amout.setBackgroundResource(R.drawable.btn_selector);
            myHold.tv_unit.setTextColor(mActivity.getResources().getColor(R.color.white));
            myHold.tv_amount.setTextColor(mActivity.getResources().getColor(R.color.white));
        } else {
            myHold.rl_amout.setBackgroundResource(R.drawable.btn_notselector);
            myHold.tv_unit.setTextColor(mActivity.getResources().getColor(R.color.main_color));
            myHold.tv_amount.setTextColor(mActivity.getResources().getColor(R.color.main_color));
        }


        if (mItemClickLitener != null) {
            myHold.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mItemClickLitener.onItemClick(myHold.itemView, myHold.getAdapterPosition());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mProducts.size();
    }

    public void setSelection(int position) {
        this.selected = position;
        notifyDataSetChanged();
    }


    class MyHold extends RecyclerView.ViewHolder {
        RelativeLayout rl_amout;
        TextView tv_amount;
        TextView tv_unit;

        public MyHold(@NonNull View itemView) {
            super(itemView);
            rl_amout = itemView.findViewById(R.id.rl_amout);
            tv_amount = itemView.findViewById(R.id.tv_amount);
            tv_unit = itemView.findViewById(R.id.tv_unit);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }
}
